/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scm.rawMaterialPurchasing.session;

import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import scm.rawMaterialPurchasing.entity.RawMaterialEntity;
import scm.rawMaterialPurchasing.entity.SupplierEntity;
import scm.rawMaterialPurchasing.entity.SupplierLineItemEntity;

/**
 *
 * @author TM
 */
@Stateless
@LocalBean
public class SupplierSessionBean {
  @PersistenceContext
    private EntityManager em;
  
    public List<String> getAllRawMaterialNames(){
     Query query = em.createQuery("SELECT r.rawMaterialName FROM RawMaterialEntity r");
     return query.getResultList();   
}
    public List<RawMaterialEntity> getAllRawMaterial(){
         Query query = em.createQuery("SELECT r FROM RawMaterialEntity r");
        return query.getResultList();   
    }
    public void save(List<SupplierLineItemEntity> supplierLineItemsList){
        for(int i=0;i<supplierLineItemsList.size();i++){
          em.persist(supplierLineItemsList.get(i));
         // supplierLineItemsList.get(i).getRawMaterial().setSupplierLineItem(supplierLineItemsList.get(i));
          em.merge(supplierLineItemsList.get(i).getRawMaterial());
        }
        em.merge(supplierLineItemsList.get(0).getSupplier());
        em.flush();
    }
    public void saveSupplier(SupplierEntity supplier){
        em.persist(supplier);
        em.flush();
    }    
    
    public SupplierEntity getSupplierById(Long id){
        
        return em.find(SupplierEntity.class, id);
    }
    
    public SupplierLineItemEntity getSupplierLineItemByID(Long id){
        
        return em.find(SupplierLineItemEntity.class, id);
    }
    
    public List<SupplierEntity> getAllSuppliers(){
        Query query = em.createQuery("SELECT s FROM SupplierEntity s");
        return query.getResultList();
    }
    
    public void deleteSupplierById(Long id){
        SupplierEntity supplier = em.find(SupplierEntity.class,id);
        em.remove(supplier);
    }
    
    public void editSupplier(SupplierEntity supplier, List<SupplierLineItemEntity> supplierLineItemsList, boolean changed){
        if (changed == true){
            //delete old supplierLineItems
            List<SupplierLineItemEntity> lineItemList = supplier.getSupplierLineItems();
            supplier.setSupplierLineItems(null);
            SupplierLineItemEntity lineItem;
            for (int i=0;i<lineItemList.size();i++){
                lineItem = em.find(SupplierLineItemEntity.class,lineItemList.get(i).getId());
                em.remove(lineItem);
            }

            for (int j=0;j<supplierLineItemsList.size();j++){
                supplierLineItemsList.get(j).setSupplier(supplier);
                em.persist(supplierLineItemsList.get(j));
            }
            em.flush();
            supplier.setSupplierLineItems(supplierLineItemsList);
        }
        em.merge(supplier);
    }
}
